﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS.AdminCP
{
    public partial class BuildMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Functions.ddlDatabinder(ddlMenuAdmin, MenuAdminsDO.MENUADMINID_FIELD, MenuAdminsDO.NAME_FIELD, new MenuAdminsBL().SelectGetByTop("","len(Level)=10",""));
             //   Ultility.ddlDatabinder(ddlLanguage, cmsLanguageDO.LANGUAGEID_FIELD, cmsLanguageDO.TITLE_FIELD, new cmsLanguageBL().SelectAll());
                gvAtDataSource();
              //  bindToDropDown(cboCategory);
            }
        }
        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int page = int.Parse(ddlPage.SelectedValue);
            switch (page)
            {
                case 1:
                    divCategory.Visible = false;
                    divArticle.Visible = false;
                    divProductCategory.Visible = false;
                    txtLinkX.Text = "/Default.aspx";
                    break;
                case 2:
                    divCategory.Visible = false;
                    divArticle.Visible = false;
                    divProductCategory.Visible = false;
                    txtLinkX.Text = "/lien-he/";
                    break;
                case 3:
                    divCategory.Visible = true;
                    divArticle.Visible = false;
                    divProductCategory.Visible = false;
                    tlCategoryDataSource();
                    break;
                case 4:
                    divCategory.Visible = true;
                    divArticle.Visible = false;
                    divProductCategory.Visible = false;
                    tlCategoryDataSource();
                    break;
                case 5:
                    divCategory.Visible = false;
                    divArticle.Visible = true;
                    divProductCategory.Visible = false;
                    cboCategory.Items.Clear();
                    ListItem item = new ListItem(".: Không chọn :.", "0");
                    cboCategory.Items.Add(item);
                    bindToDropDown(cboCategory);
                    break;
                case 6:
                    divArticle.Visible = false;
                    divCategory.Visible = false;
                    divProductCategory.Visible = true;
                    tlProductCategoryDataSource();
                    break;
                case 7:
                    divCategory.Visible = false;
                    divProductCategory.Visible = false;
                    divArticle.Visible = false;
                    txtLinkX.Visible = true;
                    txtLinkX.ReadOnly = false;
                    break;
                default:
                    break;
            }
        }
        protected void tlCategoryDataSource()
        {
            int page = int.Parse(ddlPage.SelectedValue);
            if (page == 3)
                tlCategory.DataSource = new cmsCategoryBL().SelectAdmin(false);
            else if (page == 4)
                tlCategory.DataSource = new cmsCategoryBL().SelectAdmin(true);
            tlCategory.DataBind();
            tlCategory.ExpandToLevel(3);
        }
        protected void tlProductCategoryDataSource()
        {
            tlProductCategory.DataSource = new cmsProductCategoryBL().SelectAll();
            tlProductCategory.DataBind();
            tlProductCategory.ExpandToLevel(3);
        }
        protected void tlCategory_StartNodeEditing(object sender, DevExpress.Web.ASPxTreeList.TreeListNodeEditingEventArgs e)
        {

            int id = 0;
            if (int.TryParse(tlCategory.EditingNodeKey, out id))
            {
                //   Response.Redirect("Default.aspx?Page=ArticleCategory&CategoryID=" + id.ToString());
                int page = int.Parse(ddlPage.SelectedValue);
                if (page == 3)
                    txtLinkX.Text = "/danh-muc-tin/" + Ultility.Change_AV(new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = id }).Title) + "-" + id.ToString();
                else if (page == 4)
                    txtLinkX.Text = "/noi-dung/" + Ultility.Change_AV(new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = id }).Title) + "-" +id.ToString();
            }

            tlCategoryDataSource();
        }
        protected void tlProductCategory_StartNodeEditing(object sender, DevExpress.Web.ASPxTreeList.TreeListNodeEditingEventArgs e)
        {

            int id = 0;
            if (int.TryParse(tlProductCategory.EditingNodeKey, out id))
            {
                txtLinkX.Text = "/" + Ultility.Change_AV(new cmsProductCategoryBL().Select(new cmsProductCategoryDO() { ProductCategoryID = id }).ProductCategoryTitle) + "-" + id.ToString() + "/";
            }

            tlProductCategoryDataSource();
        }
        protected void gvAtDataSource()
        {
            gvAt.DataSource = new cmsArticleBL().SelectAdmin(int.Parse(cboCategory.SelectedValue));
            gvAt.DataBind();
        }
        protected void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvAtDataSource();
        }
        protected void gvAt_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ArticleID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            txtLinkX.Text = "/bai-viet/" + Ultility.Change_AV(new cmsArticleBL().Select(new cmsArticleDO { ArticleID = ArticleID }).Title) + "-" + ArticleID.ToString();
        }
        protected void gvAt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAt.PageIndex = e.NewPageIndex;
            gvAtDataSource();
        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsCategoryBL().SelectDropDownAdmin();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["CategoryID"].ToString();
                    item.Text = "+ " + dr["Title"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["CategoryID"].ToString(), dtCate, ddl);
                }
            }
        }

        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = "-------" + dr["Title"].ToString();
                    listChild.Value = dr["CategoryID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                }
            }
        }

        protected void ddlMenuAdmin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}